function createCustomElement(type, textContent, attributes) {
	var result = document.createElement(type);
	result.textContent = textContent;

	if (attributes && attributes.length > 0) {
		for (var key in attributes) {
			var attribute = attributes[key];
			var name = attribute['name'];
			var value = attribute['value'];
			result.setAttribute(name, value);
		}
	}
	return result;
}

class WebAppObject {
	constructor(creator) {
		this.creator = creator
	}

	printCreator() {
		let message = 'В скрипте: ' + this.creator + ' вызван объект WebAppObject()'
		var li = createCustomElement('li', message, [{ "name": "style", "value": "background: rgb(81 233 88); transition: background 0.2s ease-in-out;" }]);

		function clearColor() {
			li.style.background = '';
		}
		setInterval(clearColor, 200);

		document.querySelector('.list_li').prepend(li);
	}
}

document.addEventListener('DOMContentLoaded', function () {
	let tr = createCustomElement('tr', '', []);
	var btnWebAppScriptCallContentObject = createCustomElement('button', 'Вызвать объект', [{ "name": "class", "value": "button_red" }]);
	btnWebAppScriptCallContentObject.addEventListener('click',
		function (e) {
			try {
				let webAppScriptContentObject = new ContentObject('webapp_script.js')
				webAppScriptContentObject.printCreator()
			} catch (e) {
				alert('Ошибка: ' + e)
			}
		});
	document.querySelector('.buttons_block').append(btnWebAppScriptCallContentObject);

	var btnWebAppScriptCallInjectObject = createCustomElement('button', 'Вызвать объект', [{ "name": "class", "value": "button_green" }]);
	btnWebAppScriptCallInjectObject.addEventListener('click',
		function (e) {
			try {
				let webAppScriptInjectObject = new InjectObject('webapp_script.js')
				webAppScriptInjectObject.printCreator()
			} catch (e) {
				alert('Ошибка: ' + e)
			}
		});
	document.querySelector('.buttons_block').append(btnWebAppScriptCallInjectObject);

	var btnWebAppScriptCallWebAppObject = createCustomElement('button', 'Вызвать объект', [{ "name": "class", "value": "button_green" }]);
	btnWebAppScriptCallWebAppObject.addEventListener('click',
		function (e) {
			try {
				let webAppScriptWebAppObject = new WebAppObject('webapp_script.js')
				webAppScriptWebAppObject.printCreator()
			} catch (e) {
				alert('Ошибка: ' + e)
			}
		});
	let th0 = createCustomElement('th', 'webapp_script.js', [])
	tr.append(th0)

	let th1 = createCustomElement('th', '', [])
	th1.append(btnWebAppScriptCallContentObject)
	tr.append(th1)

	let th2 = createCustomElement('th', '', [])
	th2.append(btnWebAppScriptCallInjectObject)
	tr.append(th2)

	let th3 = createCustomElement('th', '', [])
	th3.append(btnWebAppScriptCallWebAppObject)
	tr.append(th3)

	document.querySelector('.table_block').append(tr);
});